﻿using Abp;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using FwProject.Product.Dto;
using FwProject.ShopApplication;
using FwProject.ShoppingMall.Product;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FwProject.Product
{
    public class ProductTypeParAppService : FwProjectAppServiceBase, IProductTypeParAppService
    {
        private readonly IRepository<ProductType_Par, Guid> _repositoryProductTypePar;
        private readonly IRepository<ProductType_Parvalue, Guid> _repositoryProductTypeParvalue;
        private IGuidGenerator _guid { get; set; }
        public ProductTypeParAppService(IRepository<ProductType_Par, Guid> repositoryProductTypePar, IRepository<ProductType_Parvalue, Guid> repositoryProductTypeParvalue)
        {
            _repositoryProductTypePar = repositoryProductTypePar;
            _guid = SequentialGuidGenerator.Instance;
            _repositoryProductTypeParvalue = repositoryProductTypeParvalue;
        }

        public async Task UpdatedBatchAsync(Guid id, ProductTypeUpdateDto input)
        {
            //获取原来的类型参数组数据
            var original = await _repositoryProductTypePar.GetAll().Where(o => o.ProductTypeid == id).Select(o => o.Id).ToListAsync();

            #region 参数的删除
            //原来的参数
            var originalParvalue = await _repositoryProductTypeParvalue.GetAll().Where(o => original.Any(s => s == o.ProductTypeparid)).Select(o => o.Id).ToListAsync();
            //新的参数
            var newParvalue = new List<ProductTypeParvalueInput>();
            input.ProductTypeParInputs.ForEach(o =>
            {
                foreach (var item in o.ProductTypeParvalueInputs)
                {
                    newParvalue.Add(item);
                }
            });
            if (originalParvalue.Count > 0)
            {
                //获取要删除的参数
                var removes = originalParvalue.Where(o => newParvalue.Any(s => s.Id == o) == false).ToList();
                if (removes?.Count > 0)//判断是否有删除
                    for (int i = 0; i < removes.Count; i++)//执行删除操作
                    {
                        await _repositoryProductTypeParvalue.DeleteAsync(removes[i]);
                    }
            }
            #endregion

            #region 参数组的删除
            var newids = input.ProductTypeParInputs;
            if (original.Count > 0)
            {
                //获取要删除的数据
                var removes = original.Where(o => newids.Any(s => s.Id == o) == false).ToList();
                if (removes?.Count > 0)//判断是否有删除
                    for (int i = 0; i < removes.Count; i++)//执行删除操作
                    {
                        await _repositoryProductTypePar.DeleteAsync(removes[i]);
                    }
            }
            #endregion

            #region 参数组的新增
            //获取要新增的参数组
            var adds = newids.Where(r => original.Any(o => o == r.Id) == false);
            foreach (var a in adds)
            {
                a.ProductTypeid = id;
                ProductType_Par attr = a.MapTo<ProductType_Par>();
                attr.Id = _guid.Create();
                //新增操作
                var groupid = await _repositoryProductTypePar.InsertAndGetIdAsync(attr);
                #region 新增参数
                //新增参数
                if (a.ProductTypeParvalueInputs.Count > 0)
                {
                    foreach (var item in a.ProductTypeParvalueInputs)
                    {
                        item.ProductTypeparid = groupid;
                        ProductType_Parvalue parvalue = item.MapTo<ProductType_Parvalue>();
                        parvalue.Id = _guid.Create();
                        //新增参数
                        await _repositoryProductTypeParvalue.InsertAsync(parvalue);
                    }
                }
                #endregion
            }
            #endregion

            #region 添加参数
            //获取添加了参数的参数组信息
            var onlyParvalue = newids.Where(o => o.ProductTypeParvalueInputs.Where(s => s.Id == Guid.Empty).Count() > 0 && o.Id != Guid.Empty);

            foreach (var item in onlyParvalue)
            {
                foreach (var i in item.ProductTypeParvalueInputs)
                {
                    i.ProductTypeparid = item.Id;
                    ProductType_Parvalue parvalue = i.MapTo<ProductType_Parvalue>();
                    parvalue.Id = _guid.Create();
                    //新增操作
                    await _repositoryProductTypeParvalue.InsertAsync(parvalue);
                }
            }
            #endregion
        }

        public async Task<ProductTypeUpdateDto> GetEnterDtoAsync(Guid id)
        {
            var list = await _repositoryProductTypePar.GetAll().Where(o => o.ProductTypeid == id).ToListAsync();

            ProductTypeUpdateDto dto = new ProductTypeUpdateDto();
            dto.ProductTypeParInputs = list.MapTo<List<ProductTypeParInput>>();

            foreach (var item in dto.ProductTypeParInputs)
            {
                item.ProductTypeParvalueInputs = _repositoryProductTypeParvalue.GetAll().Where(o => o.ProductTypeparid == item.Id).MapTo<List<ProductTypeParvalueInput>>();
            }
            return dto;
        }

    }
}
